<?php 

	session_start();
	if(!($_SESSION["username"])||($_SESSION["role"]!="TUT")){
		//not logged in or tutor
		header("location:index.php");
	}
	
	$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
	$objDB = mysql_select_db("Project 5.5");
	
	$students=$_POST['recipients'];
	$emails=null;
	$i=0;
	//select emails for recipients
	foreach ($students as $s) {
		
		$sql="SELECT Email FROM users WHERE User_ID=".$s.";";
		$result=mysql_fetch_row(mysql_query($sql));
		$emails[$i]=$result[0];
		$i++;
	};
	
	$recipients="";
	foreach ($emails as $e) {
		$recipients.=$e.", ";
		}
	$recipients = rtrim($recipients, ", ");
	
	
	$sql="SELECT Email FROM users WHERE Username='".$_SESSION['username']."';";
	$result=mysql_fetch_row(mysql_query($sql));
	
	$sender = "From: ".$result[0];
	$subject = $_POST['subject'];
	$message = $_POST['msgtext'];
	
	echo ">>".$recipients."<<<BR/>";
	echo ">>".$sender."<<<BR/>";
	echo ">>".$subject."<<<BR/>";
	echo ">>".$message."<<<BR/>";
	
	//send email
	if (mail($recipients, $subject, $message, $sender)) {
		header("location:student-email.php?message=success");
	} else {
		header("location:student-email.php?message=fail");
	};

 ?> 